var selectedRow = 1;

function updateForm(){
	var text = $('.row_selected').children(":first").html();
	selectedRow = $('.row_selected .row-count').html();
	if(text != null)
		updateParishioner(text);
	$('#parishioner-update-form input').removeAttr("placeholder");
}

function updateParishioner(id){
	$.get('/elim/rest/parishioners/update-form/'+id,
			function(data){
		if(data!=null){
			$("#parishioner-update-form").html(data);
			createDatePickers();
			updatePhoto($('#parishioner-id').val());
		}
	}
	);
}

function updateTableItem() {
	table = $('#parishioner-data-update').dataTable();
	table.fnUpdate($('#firstName').val(), selectedRow-1, 2);
	table.fnUpdate($('#lastName').val(), selectedRow-1, 3);
	table.fnDraw();
}

function addTableItem() {
	table = $('#parishioner-data-update').dataTable();
	var index = table.fnGetData().length + 1;
	table.fnAddData( [index,
	                  $('#firstName').val(),
	                  $('#lastName').val(),
	                  '']);

	table.fnDraw();
}

$(window).load(function() {
	updatePhoto($('#parishioner-id').val());
});

require(["dataTables"], function() {
	$(document).ready(function() {

		var location = window.location.href;
		var text = location.slice(location.lastIndexOf('/')+1, location.indexOf("?"));

		if ($('#person-id').html() != text)
			updateParishioner(text);
		
		$('#parishioner-data-update').dataTable({
			"iDisplayLength": 19
		});
		$('.parishioner-data tr').live("click",function(e) {
			unselectAllRows(this, e);
			selectRow(this, e);
			updateForm();
		});
		
		$("#update-button").live("click", function(event) {
			$.post('/elim/rest/parishioners/update',
					$('form').serialize(),
					function(data){
						showResult(data);
						if (data.success == true) {
							uploadPhoto(data.id);
							updateTableItem();
						}
					}
			).error(function(error,status,errorThrown){
				handleError(error,status,errorThrown);
			});
		});			    		

		$("#create-button").live("click", function(event) {
			$.get('/elim/parishioners/create');
			$.post('/elim/rest/parishioners/create',
					$('form :input[id!="id"][id!="parishioner-id"]').serialize(),
					function(data){ 
						showResult(data);
						if (data.success == true) {
							if ($('#photo').val() == '')
								showDefaultPhoto();
							uploadPhoto(data.id);
							addTableItem();
						}
					}
			).error(function(error,status,errorThrown){
				handleError(error,status,errorThrown);
			});
		});

		$("#cancel-button").live("click", function(event) {
			if (confirm(jQuery.i18n.prop('ui.parishioners.confirm_quit')))
				listParishioners();
		});

		$("#person-fields input,textarea,select").live("change", function(event) {
			removeErrorClass(event);
			removeResultDiv();
		});

		$('#parishioner-update-form').live('keydown', 'Ctrl+s',function(event){
			$.post('/elim/rest/parishioners/update',
					$('form').serialize(),
					function(data){
						showResult(data);
					}
			).error(function(error,status,errorThrown){
				handleError(error,status,errorThrown);
			});
		});
		
		$('#photo').live('change', function(event) {
			previewPhoto(this);
		});

		$('#parishioner-update-form').bind('keydown', 'Ctrl+down',function(event){
			selectNextRow();
			updateForm();
		});

		$('#parishioner-update-form').bind('keydown', 'Ctrl+up',function(event){
			selectPreviousRow();
			updateForm();
		});
	});
	/*CTRL + down arrow*/
	$.ctrlKey(40, function() {
		selectNextRow();
		updateForm();
	}, []);
	/*CTRL + down arrow*/
	$.ctrlKey(38, function() {
		selectPreviousRow();
		updateForm();
	}, []);
});
